/**
  计算机等级考试-名单规则设置-名单规则设置
*/
<template>
  <div class="exam-create-container">
    <el-card shadow="never" v-loading="loading" body-style="padding: 24px;" class="card-margin">
      <h1 class="condition-title">{{ $t('listRuleSetting.listRule') }}</h1>
      <el-form ref="form" :model="queryForm" label-width="120px">
        <el-row :gutter="24">
          <el-col :span="8">
            <!-- 在校状态 -->
            <el-form-item :label="$t('listRuleSetting.schoolStatus') + '：'">
              <el-select v-model="queryForm.condition.leaveSchoolList" multiple filterable clearable style="width: 100%">
                <el-option
                  v-for="item in options.leaveSchoolList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('listRuleSetting.studentStatus') + '：'">
              <!-- 学籍状态 -->
              <el-select v-model="queryForm.condition.registrationStatusList" multiple filterable clearable style="width: 100%">
                <el-option
                  v-for="item in options.registrationStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!-- 入学方式 -->
            <el-form-item :label="$t('listRuleSetting.inSchoolMethod') + '：'">
              <el-select v-model="queryForm.condition.enrolMethodsList" multiple filterable clearable style="width: 100%">
                <el-option
                  v-for="item in options.enrolMethodsList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <!-- 入学年级 -->
            <el-form-item :label="$t('listRuleSetting.entryGrade') + '：'">
              <el-select v-model="queryForm.condition.gradeList" clearable multiple style="width: 100%">
                <el-option
                  v-for="item in options.gradeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="float-right" style="display: flex;">
          <el-button v-if="permitToSaveListRule" type="primary" @click="saveListRule()">{{$t('listRuleSetting.saveRule')}}</el-button>
        </div>
      </el-form>
      <h1 class="condition-title">{{ $t('listRuleSetting.studentListing') }}</h1>
      <div class="edu-opertion-bar sub-title">
        <div class="float-left">
          <el-input
            v-model.trim="queryForm.condition.name"
            :placeholder="$t('subjectCompetitionBase.studentIdOrName')"
            style="width:250px"
            @keyup.enter.native="onSubmit">
              <i slot="suffix"
                class="el-icon-search"
                style="margin-top:10px;cursor:pointer"
                @click="onSubmit">
              </i>
          </el-input>
        </div>

      </div>
      <edu-table :pagination="false" :order="false" :selection="false" :tooltipEffect="tooltipEffect" stripe :data="dataList" size="mini" @selection-change="handleSelectionChange">
        <edu-table-column :label="$t('subjectCompetitionBase.studentId')" prop="studentId"></edu-table-column>
        <edu-table-column :label="$t('subjectCompetitionBase.name')" prop="name"></edu-table-column>
        <edu-table-column :label="$t('subjectCompetitionBase.sex')" prop="sexI18n"></edu-table-column>
        <edu-table-column :label="$t('subjectCompetitionBase.college')" prop="facultyI18n"></edu-table-column>
        <edu-table-column :label="$t('listRuleSetting.schoolStatus')" prop="leaveSchoolI18n"></edu-table-column>
        <edu-table-column :label="$t('listRuleSetting.studentStatus')" prop="registrationStatusI18n"></edu-table-column>
      </edu-table>
      <el-row type="flex" class="row-bg" justify="end">
        <el-col>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryForm.pageNum_"
            :page-sizes="[20, 40, 80, 100]"
            :page-size="queryForm.pageSize_"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryForm.total_"
            style="float:right;margin-top:10px"
        ></el-pagination>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>
<script>
import UTILS from "common/src/utils/utils";
import { queryDic } from "common/src/api/dictionary";
import subjectCompetitionApi from 'common/src/api/subjectCompetition/subjectCompetition';
export default {
  components: {
  },
  data () {
    return {
      auths: [],
      permitToSaveListRule: false,
      calendarYear: '',
      listRuleId: '',
      queryForm: {
        pageNum_: 1,
        pageSize_: 20,
        total_: 0,
        condition: {
          educationTag: 1,
          excludeNewestAndExchange: 1,
          leaveSchoolList: [],
          registrationStatusList: [],
          enrolMethodsList: [],
          gradeList: [],
          name: ''
        }
      },
      options: {
        leaveSchoolList: [],
        registrationStatusList: [],
        enrolMethodsList: [],
        gradeList: []
      },
      dataList: []
    }
  },
  async created() {
    // 获取当前用户auths权限
    if (localStorage.getItem("sessiondata")) {
      if (JSON.parse(localStorage.getItem("sessiondata")).auths) {
        this.auths = JSON.parse(localStorage.getItem("sessiondata")).auths
      }
    }
    this.initOptions()
    // 获取规则
    const currentRuleRes = await subjectCompetitionApi.getCurrentTermRuleLists()
    if (currentRuleRes.code === 200 && currentRuleRes.data) {
      this.listRuleId = currentRuleRes.data.id
      this.queryForm.condition.leaveSchoolList = currentRuleRes.data.leaveSchoolList ? currentRuleRes.data.leaveSchoolList : []
      this.queryForm.condition.registrationStatusList = currentRuleRes.data.registrationList ? currentRuleRes.data.registrationList : []
      this.queryForm.condition.enrolMethodsList = currentRuleRes.data.enrolMethodsList ? currentRuleRes.data.enrolMethodsList : []
      this.queryForm.condition.gradeList = currentRuleRes.data.currentGradeList ? currentRuleRes.data.currentGradeList : []
      await this.getGradeInfo(true)
      this.onSubmit()
    } else {
      await this.getGradeInfo(false)
    }
  },
  methods: {
    async calendarRole(){
      const currentRuleRes = await subjectCompetitionApi.getCurrentTermRuleLists()
       if (currentRuleRes.code === 200 && currentRuleRes.data) {
         this.listRuleId = currentRuleRes.data.id;
       }
    },
    initOptions() {
      // 筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: [
          "K_ZXZT", // 在校状态
          "G_XJZT", // 学籍状态
          "X_RXFS" // 入学方式
        ]
      }
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            leaveSchoolList: "K_ZXZT", // 在校状态
            registrationStatusList: "G_XJZT", // 学籍状态
            enrolMethodsList: "X_RXFS" // 入学方式
          },
          res.data,
          this.options
        )
      })
    },
    // 获取年级信息
    async getGradeInfo(hasSetting) {
      // 获取当前入学年级信息
      const calendarInfo = await subjectCompetitionApi.getCurrentDate()
      if (calendarInfo.code === 200) {
        if (!hasSetting) {
          this.queryForm.condition.gradeList = [calendarInfo.data.calendarYear]
          this.calendarYear = calendarInfo.data.calendarYear
        }
        calendarInfo.data.calendarYearList.forEach(el => {
          this.options.gradeList.push({
            value: el.toString(),
            label: el.toString()
          })
        })
      }
    },
    getListData() {
      subjectCompetitionApi.selectStudentRoster(this.queryForm).then((res) => {
        this.dataList = res.data.list
        this.queryForm.total_ = res.data.total_
      })
    },
    onSubmit() {
      this.queryForm.pageNum_ = 1
      this.getListData();
      this.calendarRole();
    },
    // 保存规则
    saveListRule() {
      subjectCompetitionApi.saveRuleList({
        id: this.listRuleId,
        leaveSchoolList: this.queryForm.condition.leaveSchoolList ? this.queryForm.condition.leaveSchoolList : [],
        registrationList: this.queryForm.condition.registrationStatusList ? this.queryForm.condition.registrationStatusList : [],
        enrolMethodsList: this.queryForm.condition.enrolMethodsList ? this.queryForm.condition.enrolMethodsList : [],
        currentGradeList: this.queryForm.condition.gradeList ? this.queryForm.condition.gradeList : [],
        examCategory: 6
      }).then(res => {
        this.$message.success(this.$t('subjectCompetitionBase.savedSuccessfully'))
        this.onSubmit()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleSizeChange(val) {
      this.queryForm.pageSize_ = val
      this.getListData()
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum_ = val
      this.getListData()
    },
    beforeClose() {
      this.$refs['dialogForm'].clearValidate()
      this.dialogVisible = false
    }
  },
  watch: {
    auths: {
      handler(newVal) {
        if (newVal && Array.isArray(newVal) && newVal.length > 0) {
          newVal.some(item => {
            if (item.authCode === 'computer-list-rule-setting-save') {
              this.permitToSaveListRule = true
            }
          })
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
